import request from '@/router/axios';
import Mock from 'mockjs';

let _list = [
    {
        id:1,
        name:'热门手机秒杀活动',
        starTime:'2022-04-18',
        endTime:'2022-04-22',
        status:"0",//0上线  1下线
    },
    {
        id:2,
        name:'热门口红秒杀活动',
        starTime:'2022-04-19',
        endTime:'2022-04-20',
        status:"1",//0上线  1下线
    },
    {
        id:3,
        name:'热门家电秒杀活动',
        starTime:'2022-04-10',
        endTime:'2022-04-15',
        status:"1",//0上线  1下线
    },
    {
        id:4,
        name:'热门运动鞋秒杀活动',
        starTime:'2022-04-17',
        endTime:'2022-04-21',
        status:"0",//0上线  1下线
    },
    {
        id:5,
        name:'热门首饰秒杀活动',
        starTime:'2022-04-20',
        endTime:'2022-04-22',
        status:"0",//0上线  1下线
    },
    {
        id:6,
        name:'热门面膜秒杀活动',
        starTime:'2022-04-18',
        endTime:'2022-04-22',
        status:"0",//0上线  1下线
    }
]

// 获取秒杀数据列表seckill
export const seckillList = (data) =>request({
    url:'/seckillList',
    method:'get',
    data
})
Mock.mock(/\/seckillList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize,
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})
//添加秒杀活动
export const addSeckill = (data) =>request({
    url:'/addSeckill',
    mothed:'post',
    data
});
Mock.mock(/\/addSeckill/,config=>{
    var ids = Mock.mock({"id|7-99":1000})
    let datas = JSON.parse(config.body)
    let {name,starTime,endTime,status} = datas;
    if(name && starTime && endTime && status){
        _list.unshift({
            id:ids.id,
            name,
            starTime,
            endTime,
            status, 
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除数据
export const detSeckill = (data) =>request({
    url:'/detSeckill',
    mothed:'delete',
    data
})
Mock.mock(/\/detSeckill/,config=>{
    let {id} = JSON.parse(config.body);
    if(id){
        for(let index in _list){
            if(_list[index].id==id){
                _list.splice(index,1)
            }
        }
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//查询数据 根据活动名称
export const querySeckill = (data) =>request({
    url:'/querySeckill',
    mothed:'get',
    data
})
Mock.mock(/\/querySeckill/,config=>{
    let datas = JSON.parse(config.body);
    let arr = [];
    if(datas.name){
        if(datas.name){
            arr = _list.filter((item,index)=>item.name.indexOf(datas.name)!=-1);
        }
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// 修改数据
export const editSeckill = (data) =>request({
    url:'/editSeckill',
    mothed:'put',
    data
})
Mock.mock(/\/editSeckill/,config=>{ 
    let datas = JSON.parse(config.body);
    let {id} = datas;
    if(id){
        if(datas.name || datas.starTime || datas.endTime ){
            for(let item of _list){
                if(item.id==id){
                    item.name = datas.name?datas.name:item.name;
                    item.starTime = datas.starTime?datas.starTime:item.starTime
                    item.endTime = datas.endTime?datas.endTime:item.endTime;
                  
                }
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

// 修改是否上线
export const editline = (data) =>request({
    url:'/editline',
    mothed:'put',
    data
})
Mock.mock(/\/editline/,config=>{ 
    let datas = JSON.parse(config.body);
    let {id,status} = datas;
    if(id && status){
        _list[_list.findIndex(item=>item.id==id)].status = datas.status;
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})